package com.ojoin.trade.supplyloan_core.web.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlStattaskRunresultDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlStatetaskRunresultvo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlStattaskRunresult;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-10-22
 * Time: 上午11:48
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbSlStattaskRunresultDaoImpl extends BaseDaoImpl<TbSlStattaskRunresult> implements TbSlStattaskRunresultDao {
    @Override
    public void insertTbSlStattaskRunresult(TbSlStattaskRunresult runresultvo) throws BusinessException {
        String mapMethodName ="insert";
        try{
            this.sqlSessionTemplate.insert(getSqlName(mapMethodName),runresultvo);
        }catch (Exception e) {
            throw new DaoException(String.format("插入商户的定时任务总的统计信息表出错joinid==="+runresultvo.getIdOfTbMerchantUser()+"语句：%s",
                    getSqlName(mapMethodName)), e);
        }

    }

    @Override
    public List<TbSlStattaskRunresult> queryTbSltaskRunresultByJoinidanddate(int joinid, String check_date) throws BusinessException {
        String methodName="selectByJoinidanddate";
        HashMap<String,Object> parammap=new HashMap<String, Object>();
        parammap.put("id_of_tb_merchant_user",joinid);
        parammap.put("task_exec_date",check_date);
        try {
        	List<TbSlStattaskRunresult> list = this.sqlSessionTemplate.selectList(getSqlName(methodName),
                    parammap);
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("根据商户的joinid查询商户日定时任务统计信息表出错->" + joinid + "出错！语句：%s",
                    getSqlName(methodName)), e);
        }
        return null;
    }

    @Override
    public List<TbSlStatetaskRunresultvo> queryAllTbSltaskRunresultBydate(String task_exec_date) throws BusinessException {
        String methodName="selectAllTbSlStatetask";
        try {
        	List<TbSlStatetaskRunresultvo> list = this.sqlSessionTemplate.selectList(getSqlName(methodName),
                    task_exec_date);
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户日定时任务统计信息表出错->" + task_exec_date + "出错！语句：%s",
                    getSqlName(methodName)), e);
        }
        return null;
    }

    @Override
    public List<TbSlStatetaskRunresultvo> queryAllFailureTbSltaskRunresultBydate(String task_exec_date) throws BusinessException {
        String methodName="selectAllTbSlStateFailuretask";
        try {
        	List<TbSlStatetaskRunresultvo> list = this.sqlSessionTemplate.selectList(getSqlName(methodName),
                    task_exec_date);
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户日运行失败统计信息表出错->" + task_exec_date + "出错！语句：%s",
                    getSqlName(methodName)), e);
        }
        return null;
    }

    @Override
    public List<TbSlStatetaskRunresultvo> queryAllSuccessTbSltaskRunresultBydate(String task_exec_date) throws BusinessException {
        String methodName="selectAllTbSlStateSuccesstask";
        try {
        	List<TbSlStatetaskRunresultvo> list = this.sqlSessionTemplate.selectList(getSqlName(methodName),
                    task_exec_date);
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户日运行成功统计信息表出错->" + task_exec_date + "出错！语句：%s",
                    getSqlName(methodName)), e);
        }
        return null;
    }

	@Override
	public List<TbSlStatetaskRunresultvo> queryAllTbSltaskRunresultBydate(
			String check_date, Pageable page) throws BusinessException {
		String methodName="selectAllTbSlStatetask";
        try {
        	List<TbSlStatetaskRunresultvo> list = this.sqlSessionTemplate.selectList(getSqlName(methodName),
            		check_date,this.getPageBounds(page)); 
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户日定时任务统计信息表出错->" +check_date + "出错！语句：%s",
                    getSqlName(methodName)), e);
        }
        return null;
	}

	@Override
	public List<TbSlStatetaskRunresultvo> queryByPlatformAndMerchantUserIDAndDate(
			TbSlStattaskRunresult tbs,
			Pageable page) throws BusinessException {
		  
		String methodName="selectByPlatformAndMerchantUserIDAndDate";
		
        try {
            List<TbSlStatetaskRunresultvo> list = this.sqlSessionTemplate.selectList(getSqlName(methodName),
            		tbs ,this.getPageBounds(page)); 
            if (list != null && list.size() >0)
                return list; 
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户日定时任务统计信息表出错->" +JSONUtils.toJSONString(tbs) + "出错！语句：%s",
                    getSqlName(methodName)), e);
        }
		return null;
	}

    @Override
    public List<TbSlStattaskRunresult> queryFailureByJoinid(int joinid, String curdate) throws BusinessException {
        String methodName="selectFailureByJoinid";
        Map<String,Object> map=new HashMap<>();
        map.put("id_of_tb_merchant_user",joinid);
        map.put("task_exec_date",curdate);

        try {
            List<TbSlStattaskRunresult> list = this.sqlSessionTemplate.selectList(getSqlName(methodName),
                   map);
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户日出错的统计信息有误-> 出错！语句：%s",
                    getSqlName(methodName)), e);
        }
        return null;
    }

	@Override
	public void deleteByPlateFormAndTime(int platformUserId, String time)
			throws BusinessException {
		
		HashMap<String,Object> map=new HashMap<String, Object>();
        String methodname="deleteByPlatformUserIDAndDate";
        map.put("idOfPlatformUser",platformUserId);
        map.put("taskExecDate",time);
         
        try {
            this.sqlSessionTemplate.delete(getSqlName(methodname),
                    map);
        } catch (Exception e) {
            throw new DaoException(String.format("删除商户电商平台总统计结果情况信息表出错->出错！语句：%s",
                    getSqlName(methodname)), e);
        }
		
	}

	@Override
	public void updateByIdOfPlatformUserAndtaskExecDate(
			TbSlStattaskRunresult runresultvo) throws BusinessException {
		this.sqlSessionTemplate.update("updateByIdOfPlatformUserAndtaskExecDate", runresultvo);
		
	}
}
